<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select v-model="listQuery.type" placeholder="类型" clearable style="width: 120px" class="filter-item">
        <el-option :key="1" label="课件" :value="1" />
        <el-option :key="2" label="笔记" :value="2" />
      </el-select>
      <el-select v-model="listQuery.educationLevel" placeholder="教育阶段" clearable style="width: 120px" class="filter-item">
        <el-option :key="1" label="小学" :value="1" />
        <el-option :key="2" label="初中" :value="2" />
        <el-option :key="3" label="高中" :value="3" />
        <el-option :key="4" label="大学" :value="4" />
      </el-select>
      <el-select v-model="listQuery.subjectId" placeholder="学科" clearable style="width: 120px" class="filter-item" filterable>
        <el-option
          v-for="item in subjectOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        添加课件笔记
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-magic-stick" @click="handleAIGenerate">
        智能生成
      </el-button>
    </div>

    <div class="material-grid">
      <el-card 
        v-for="item in list" 
        :key="item.id" 
        class="material-card"
        shadow="hover"
        @click.native="handleDetail(item)"
      >
        <div class="material-cover">
          <img :src="item.coverUrl || defaultCover(item.type)" alt="封面">
          <div class="material-type">{{ item.typeName }}</div>
        </div>
        <div class="material-info">
          <h3 class="material-title">{{ item.title }}</h3>
          <div class="material-meta">
            <span>{{ item.subjectName }}</span>
            <span>{{ item.educationLevelName }}</span>
          </div>
          <div class="material-footer">
            <span>{{ formatDate(item.createTime) }}</span>
            <div class="material-actions">
              <el-button type="text" size="mini" @click.stop="handleUpdate(item)">编辑</el-button>
              <el-button type="text" size="mini" @click.stop="handleDelete(item)">删除</el-button>
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <div v-if="!list || list.length === 0" class="empty-data">
      暂无数据
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getMaterialList, deleteMaterial } from '@/api/material';
import { getAllSubjects } from '@/api/subject';
import Pagination from '@/components/Pagination';

export default {
  name: 'Materials',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 12,
        keyword: '',
        type: undefined,
        educationLevel: undefined,
        subjectId: undefined
      },
      subjectOptions: []
    };
  },
  created() {
    this.getList();
    this.getSubjects();
  },
  methods: {
    getList() {
      this.listLoading = true;
      getMaterialList(this.listQuery)
        .then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    getSubjects() {
      getAllSubjects()
        .then(response => {
          const subjects = response.data;
          this.subjectOptions = subjects.map(subject => ({
            value: subject.id,
            label: subject.name
          }));
        })
        .catch(() => {
          this.$message.error('获取学科列表失败');
        });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleCreate() {
      this.$router.push('/material/create');
    },
    handleAIGenerate() {
      this.$router.push('/ai-material-generate');
    },
    handleUpdate(row) {
      this.$router.push(`/material/edit/${row.id}`);
    },
    handleDetail(row) {
      this.$router.push(`/material/detail/${row.id}`);
    },
    handleDelete(row) {
      this.$confirm('确认删除该课件笔记?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteMaterial(row.id)
            .then(() => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getList();
            })
            .catch(() => {
              this.$message({
                type: 'error',
                message: '删除失败!'
              });
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    formatDate(date) {
      if (!date) return '';
      const d = new Date(date);
      return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
    },
    defaultCover(type) {
      // 使用内联的数据URL代替外部图片文件
      return type === 1 
        ? '' 
        : '';
    }
  }
};
</script>

<style scoped>
.filter-container {
  padding-bottom: 20px;
}
.filter-item {
  margin-right: 10px;
}
.material-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}
.material-card {
  cursor: pointer;
  transition: all 0.3s;
}
.material-card:hover {
  transform: translateY(-5px);
}
.material-cover {
  position: relative;
  height: 150px;
  overflow: hidden;
}
.material-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.material-type {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: rgba(64, 158, 255, 0.8);
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}
.material-info {
  padding: 10px;
}
.material-title {
  margin: 0 0 10px;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.material-meta {
  display: flex;
  justify-content: space-between;
  color: #909399;
  font-size: 12px;
  margin-bottom: 10px;
}
.material-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #909399;
  font-size: 12px;
}
.material-actions {
  display: flex;
}
.empty-data {
  text-align: center;
  padding: 40px 0;
  color: #909399;
  font-size: 14px;
}
</style> 